package com.haier.cre.commonframe.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;

import com.blankj.utilcode.util.Utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 项目名：CommonLibrary
 * 包 名：com.haier.cre.commonframe.utils
 * 创建人：18002063
 * 日 期：2018/10/16
 * 简 述：日期时间工具类
 */
public class DateTimeUtils {

    public static final DateFormat df_Hm = new SimpleDateFormat("HH:mm");
    public static final DateFormat df_y = new SimpleDateFormat("yyyy");
    public static final DateFormat df_yM = new SimpleDateFormat("yyyy-MM");
    public static final DateFormat df_yMd = new SimpleDateFormat("yyyy-MM-dd");
    public static final DateFormat dfyMd = new SimpleDateFormat("yyyy.MM.dd");
    public static final DateFormat df_yMdHm = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");
    public static final DateFormat df_yMdHms = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    public static String Hm = "HH:mm";
    public static String y = "yyyy";
    public static String yM = "yyyy-MM";
    public static String YMd = "yyyy-MM-dd";
    public static String yMd = "yyyy.MM.dd";
    public static String yMdHm = "yyyy-MM-dd HH:mm";
    public static String yMdHms = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取两个时间的差值
     *
     * @param str
     * @return
     */
    public static String replaceData(String str) {
        try {
            Date now = new Date();
            Date d = df_yMdHm.parse(str);
            str = twoDateDistance(d, now);
        } catch (ParseException e) {
            //e.printStackTrace();
        }
        return str;
    }

    /******
     * 计算两个日期型的时间相差多少时间
     *
     * @param startDate
     * @param endDate
     * @return
     * @frmat yyyy-mm-dd&T&hh:mm:ss.sss+08:00
     */
    @SuppressLint("SimpleDateFormat")
    public static String twoDateDistance(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return null;
        }
        long timeLong = endDate.getTime() - startDate.getTime();
        if (timeLong < 60 * 1000)
            return timeLong / 1000 + "秒前";
        else if (timeLong < 60 * 60 * 1000) {
            timeLong = timeLong / 1000 / 60;
            return timeLong + "分钟前";
        } else if (timeLong < 60 * 60 * 24 * 1000) {
            timeLong = timeLong / 60 / 60 / 1000;
            return timeLong + "小时前";
        } else {
            return df_yMd.format(startDate);
        }
    }

    /**
     * 计算两个时间相差多少分钟
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param format    时间格式
     * @return
     */
    public static String dateDiff(String startTime, String endTime, String format) {
        try {

            //按照传入的格式生成一个simpledateformate对象
            SimpleDateFormat sd = new SimpleDateFormat(format);
            long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
            long nh = 1000 * 60 * 60;//一小时的毫秒数
            long nm = 1000 * 60;//一分钟的毫秒数
            long ns = 1000;//一秒钟的毫秒数long diff;try {
            //获得两个时间的毫秒时间差异
            long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            long day = diff / nd;//计算差多少天
            long hour = diff % nd / nh;//计算差多少小时
            long min = diff % nd % nh / nm;//计算差多少分钟
            long sec = diff % nd % nh % nm / ns;//计算差多少秒//输出结果
            L.d("时间相差：", day + "天" + hour + "小时" + min + "分钟" + sec + "秒。");
            return min + "";
        } catch (ParseException e) {
            //e.printStackTrace();
            return "";
        }
    }

    /**
     * 判断两个日期是否在同一周内
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeekDate(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = format.parse(date1);
            d2 = format.parse(date2);
            Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTime(d1);
            cal2.setTime(d2);
            int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
            //subYear==0,说明是同一年
            if (subYear == 0) {
                if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                    return true;
            } else if (subYear == 1 && cal2.get(Calendar.MONTH) == 11) {
                if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                    return true;
            } else if (subYear == -1 && cal1.get(Calendar.MONTH) == 11) {
                if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                    return true;
            }
            return false;
        } catch (Exception e) {
            //e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断两个日期是否在同一月内
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMonthDate(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = format.parse(date1);
            d2 = format.parse(date2);
            Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTime(d1);
            cal2.setTime(d2);
            int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
            //subYear==0,说明是同一年
            if (subYear == 0) {
                if (cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH))
                    return true;
            }
            return false;
        } catch (Exception e) {
            //e.printStackTrace();
            return false;
        }
    }

    /**
     * 比较两个日期是否相同
     *
     * @param d1
     * @param d2
     * @return 前者等于后者返回true 反之false
     */
    public static boolean compareDate(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);

        int result = c1.compareTo(c2);
        if (result == 0)
            return true;
        else
            return false;
    }

    /**
     * 根据毫秒值获取时间
     *
     * @param time
     * @return
     */
    public static String formLongToDate(long time, String type) {
        try {
            Date d = new Date(time);
            SimpleDateFormat sdf = new SimpleDateFormat(type);
            return sdf.format(d);
        } catch (Exception e) {
            //e.printStackTrace();
            return "";
        }
    }

    /**
     * 根据时间获取毫秒值
     *
     * @param date
     * @return
     */
    public static long formDateToLong(String date, String type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        try {
            return simpleDateFormat.parse(date).getTime();
        } catch (ParseException e) {
            //e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        try {
            Date dt = new Date();
            return df_yMd.format(dt);
        } catch (Exception e) {
            //e.printStackTrace();
            return "";
        }
    }

    /**
     * 判断当前日期是星期几
     *
     * @param pTime 修要判断的时间
     * @return dayForWeek 判断结果
     */
    public static int dayForWeek(String pTime) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(format.parse(pTime));
            int dayForWeek = 0;
            if (c.get(Calendar.DAY_OF_WEEK) == 1) {
                dayForWeek = 7;
            } else {
                dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
            }
            return dayForWeek;
        } catch (ParseException e) {
            //e.printStackTrace();
            return 0;
        }
    }

    /**
     * 处理ITSM时间
     *
     * @param timeData
     * @return
     */
    public static String dealITSMTime(String timeData) {
        if (!TextUtils.isEmpty(timeData)) {
            if (timeData.contains("T") && timeData.contains("+08:00")) {
                String timeStr = timeData.replace("+08:00", "");
                String[] resultStr = timeStr.split("T");
                if (null != resultStr && resultStr.length > 0) {
                    return resultStr[0] + " " + resultStr[1];
                } else {
                    return "";
                }
            } else {
                return timeData;
            }
        } else {
            return "";
        }
    }

    /**
     * 把数字转换为时间显示
     *
     * @param number
     * @return
     */
    public static String transNumberToDate(float number) {
        try {
            if (number < 60) {
                return "00:00:" + judgeTwo((int) number);
            } else if (number >= 60 && number <= 60 * 60 - 1) {
                int minutes = (int) (number / 60);
                int seconds = (int) (number - minutes * 60);
                return "00:" + judgeTwo(minutes) + ":" + judgeTwo(seconds);
            } else if (number > 60 * 60 - 1 && number <= 24 * 60 * 60 - 1) {
                int hours = (int) (number / 60 / 60);
                int minutes = (int) ((number - hours * 60 * 60) / 60);
                int seconds = (int) (number - hours * 60 * 60 - minutes * 60);
                return judgeTwo(hours) + ":" + judgeTwo(minutes) + ":" + judgeTwo(seconds);
            } else if (number > 24 * 60 * 60 - 1) {
                return "23:59:59";
            } else {
                return "00:00:00";
            }
        } catch (Exception e) {
            //e.printStackTrace();
            return "00:00:00";
        }
    }

    /**
     * 把时间转换为数字显示
     *
     * @param allTime
     * @return
     */
    public static float transDateToNumber(String allTime) {
        try {
            if (!TextUtils.isEmpty(allTime)) {
                String[] str = allTime.split(":");
                float hours = Float.parseFloat(str[0]);
                float minutes = Float.parseFloat(str[1]);
                float seconds = Float.parseFloat(str[2]);
                return 60 * 60 * hours + 60 * minutes + seconds;
            } else {
                return 0.0f;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            return 0.0f;
        }
    }

    /**
     * 判断数字是否大于10
     *
     * @param numberValue
     * @return
     */
    public static String judgeTwo(int numberValue) {
        try {
            if (numberValue < 10) {
                return "0" + numberValue;
            } else {
                return "" + numberValue;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            return "";
        }
    }
}
